<?php
namespace app\common\model;

use think\Model;

class Task extends Model
{
    // 指定表名,不含前缀
    protected $name = 'task';
    /* 获取USER表中人所有用户到任务User表 */

    public function getList($admin_reg,$taskid=0){
        $condition = '';
        if ($taskid!=0)$condition = " AND id=".$taskid;
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $arr=$this->field('id,task_name')->where(['admin_reg'=>['in',$child_plaform]])->where('status=1 and isdelete=0 '.$condition)->select();
        return $arr;
    }

    public function getAllUserToBasic($trainingid, $userid = 0, $status, $company_id,$task_type) {
        if (empty($trainingid))
            return false;
        $time = time();
        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ex_task_user(identity_id,task_id,task_completeness,is_complete,task_type,create_time) ";
        $sql = "(select b.id identity_id,{$trainingid} taskid,0 task_completeness,0 is_complete,a.userid gdl_userid,ep.id plaform_id,{$task_type} task_type,{$time} time,b.admin_reg from ex_user a left join ex_user_company b on a.userid = b.userid  left join ex_plaform as ep on b.admin_reg=ep.admin_reg  where a.isdelete=0 and a.status=1 and b.admin_reg  in ({$cplaform}) ";

        if (is_array($userid)) {
            $userid = implode(',', $userid);
        } else {
            $userid = 0;
        }
        if (!$status) { //等于1表示所有
            $sql .= ' and b.id in(' . $userid . ')';
        }

        $sql .= "and b.id not in (select identity_id from ex_task_user where task_id='{$trainingid}' ";

        if (!empty($userid))
            $sql .= " and b.id in(" . $userid . ")";
        $sql .= "))";
        $res = $this->query($sql);

        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from ex_event_user where eventid='{$trainingid}')";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
//        var_dump($res);die;
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",".$v['taskid'].",".$v['task_completeness'].",".$v['is_complete'].",".$v['task_type'].",'".$v['time']."','".$v['admin_reg']."'),";
            }

            $valueStr = rtrim($value_str,',');

            $sql2 = "INSERT into ex_task_user(identity_id,userid,plaform_id,task_id,task_completeness,is_complete,task_type,create_time,admin_reg) values $valueStr";
//              var_dump($sql2);die;
            $this->execute($sql2);
        }
//        $this->execute($sql);
        return true;
    }

    /* 获取USER表中人满足条件用户到任务User表 */

    public function getAllUserToMARK($trainingid, $userid = 0, $status, $company_id,$task_type) {
        if (empty($trainingid)) {
            return false;
        }
        $time = time();
        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ex_task_user(identity_id,task_id,task_completeness,is_complete,task_type,create_time) ";
        $sql = "(select b.id identity_id,{$trainingid} taskid,0 task_completeness,0 is_complete,a.userid gdl_userid,ep.id plaform_id,{$task_type} task_type,{$time} time,b.admin_reg from ex_user a left join ex_user_company b on a.userid = b.userid  left join ex_plaform as ep on b.admin_reg=ep.admin_reg  where a.isdelete=0 and a.status=1 and b.admin_reg  in ({$cplaform})  "; //排除超级管理员


        switch ($status) {
            case '3':
                if ($userid) {
                    $sql .= ' and project_id=' . $userid;
                }
                break;

            case '4':
                $sql .= ' and specialty_id=' . $userid;
                break;
        }

        $sql .= " and b.id not in (select identity_id from ex_task_user where task_id='{$trainingid}' ";


        $sql .= "))";
        $res = $this->query($sql);
        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from ex_event_user where eventid='{$trainingid}')";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
//        var_dump($res);die;
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",".$v['taskid'].",".$v['task_completeness'].",".$v['is_complete'].",".$v['task_type'].",'".$v['time']."','".$v['admin_reg']."'),";
            }

            $valueStr = rtrim($value_str,',');

            $sql2 = "INSERT into ex_task_user(identity_id,userid,plaform_id,task_id,task_completeness,is_complete,task_type,create_time,admin_reg) values $valueStr";
//              var_dump($sql);die;
            $this->execute($sql2);
        }
        return true;
    }

    public function getChildAdminReg(){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $cplaform = '1';
        if (count($child_plaform)>1){
            foreach ($child_plaform as $kc=>$kv)
            {
                $cplaform .=",'".$kv."' ";
            }
            $cplaform .=",'".$kv."'";
        }else{
            $cplaform = implode(',',$child_plaform);
            $cplaform = "'".$cplaform."'";
        }
        return $cplaform;
    }
	
	public function getListByArgOne(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->find();
        return $arg;
    }
}
